<?php
/**
 * 消费记录查询
 * @author Administrator
 *
 */
class pay extends CControllerOwnerLoginBase{
	
	public function show()
	{
		$type = $this->getRequestData("sel_type","get",1);;
		$pageSize = 10;
		$page = $this->getRequestData("page","get",1);
		
		$s_time=$this->getRequestData("txt_s_time","get",date("Y-m"."-01"));
		$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
		$shop_name=$this->getRequestData("txt_shop_name","get","");
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));
		
		$where = "ic.shop_id=t.shop_id and is_cancel=0 and owner_id=".$this->_ownerID." and cost_type=".$type;
		
		$where .= " and cost_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and cost_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";
		
		$urlParam["txt_s_time"] = $s_time;
		$urlParam["txt_e_time"] = $e_time_old;
		$urlParam["sel_type"] = $type;
		
		if($shop_name)
		{
			$where .= " and t.shop_name like '%".$shop_name."%'";
			$urlParam["txt_shop_name"] = $shop_name;
		}
		
		$obj = new integralChangeModel();
		$c = 0;
		
		$v = $type==1?"add":($type==2?"reduce":"cancel");
		
		$list = $obj->selectPage($page,$pageSize,"cost_time,cancel_cost_type,cost_money_t,cost_shop_before_money,cost_shop_after_money,ic.shop_id,shop_name,discount_type,discount_num",
				$where,"c_id desc","",$c,"t_integral_change ic, t_shop t"
		);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_card","txt_owner_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$pageStr = CPage::showPage($page,$pageSize,$c,$pageUrl);
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		$this->render("pay/".$v,array("pageStr"=>$pageStr,"data"=>$list,"s_time"=>$s_time,
			"e_time"=>$e_time_old,"shop_name"=>$shop_name,"type"=>$type));
	}
	
	public function add()
	{
		$this->showList(1);
	}
	 
	public function reduce()
	{
		$this->showList(2);
	}
	
	private function showList($type)
	{
		
	}
	
	public function detail()
	{
		$shopID = $this->getRequestData("id","get");
		
		if(!$shopID || !CCommon::checkNum($shopID))
		{
			$this->logout();
		}
		
		$shopModel = new shopModel();
		
		$shopInfo = $shopModel->selectByWhere(
			"t_shop t, t_owner_shop os,t_shop_product sp",
			"t.shop_name,t.shop_id,t.shop_status,os.balance,os.owner_card_status,sp.pic_path",
			array(),"t.shop_id=os.shop_id and t.shop_id=sp.shop_id and t.shop_id=".$shopID." and os.owner_id=".$this->_ownerID
		);
		
		$s_time=$this->getRequestData("txt_s_time","get",date("Y-m"."-01"));
		$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
		$type=$this->getRequestData("sel_cost_type","get","");
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));
		
		$where = "shop_id=".$shopID." and owner_id=".$this->_ownerID;
		
		$where .= " and cost_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and cost_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";
		
		if($type==1 || $type==2 || $type==3)
		{
			$where .= " and cost_type=".$type;
		}
		
		$icModel = new integralChangeModel();
		
		$pageSize = 10;
		$page = $this->getRequestData("page","get",1);
		
		$c = 0;
		
		$list = $icModel->selectPage($page,$pageSize,"cost_time,cost_type,cost_money_t,cost_shop_after_money,discount_type,discount_num",
				$where,"c_id desc","",$c,"t_integral_change"
		);
		
		$pageStr = CPage::showPage($page,$pageSize,$c,"?".$_SERVER["QUERY_STRING"]);
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		
		$this->render("pay/detail",
			array("shopInfo"=>$shopInfo,
					"shopID"=>$shopID,
					"s_time"=>$s_time,
					"e_time"=>$e_time_old,
					"type"=>$type,
					"data"=>$list,
					"pageStr"=>$pageStr));
	}
	
	public function op_status()
	{
		$type=$this->getRequestData("type");
		$pwd=$this->getRequestData("pwd");
		$shopID=$this->getRequestData("shopID");
		
		if($type!=1 && $type!=2)
		{
			echo -10;
			exit;
		}
		
		$pwd = CCommon::pwdMd5($pwd);
		
		$ownerModel = new ownerModel();
		if($ownerModel->checkPayPwdByOwner($this->_ownerID,$pwd))
		{
			if($ownerModel->changeCardStatusForShop($this->_ownerID,$shopID,$type))
			{
				echo 1;
			}
			else{
				echo -2; //修改失败
			}
		}
		else{
			echo -1; //密码不正确
		}
		
		exit;
	}
}